package org.wuqi.microweb.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.wuqi.microweb.annotation.LoginJWT;

/**
 * @author jjj
 * @program: earnest-money
 * @description:TODO
 * @date 2024-01-10 10:42:54
 */
@Component
public class JwtInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception {
        System.out.println("prehandler");
        if (request.getMethod().toUpperCase().equals("OPTIONS")) {
            return true;
        }
        String token = request.getHeader("Authorization");
        if (token == null||"".equals(token)){
            throw new Exception();
        }
        if (handler instanceof HandlerMethod){
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            LoginJWT methodAnnotation = handlerMethod.getMethodAnnotation(LoginJWT.class);
            System.out.println(methodAnnotation.required());
        }
        return true;
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion...");
    }

}
 
 